

package com.hazelcast.jet.impl.processor;

import com.hazelcast.jet.core.AbstractProcessor;
import com.hazelcast.jet.core.Inbox;
import com.hazelcast.jet.impl.util.AutoCloseableTraverser;

import javax.annotation.Nonnull;
import java.util.function.Function;

/**
 * Same as {@link TransformBatchedP}. This processor will close the internal traverser
 */
public class TransformBatchedAutoCloseableP<T, R> extends AbstractProcessor {

    private final Function<? super Iterable<T>, ? extends AutoCloseableTraverser<? extends R>> mapper;

    private AutoCloseableTraverser<? extends R> outputTraverser;

    private boolean isCooperative = true;

    public TransformBatchedAutoCloseableP(Function<? super Iterable<T>, ? extends AutoCloseableTraverser<? extends R>> mapper) {
        this.mapper = mapper;
    }

    @Override
    public boolean isCooperative() {
        return isCooperative;
    }

    public TransformBatchedAutoCloseableP<T, R> setCooperative(boolean cooperative) {
        isCooperative = cooperative;
        return this;
    }

    @Override
    @SuppressWarnings("unchecked")
    public void process(int ordinal, @Nonnull Inbox inbox) {
        if (outputTraverser == null) {
            outputTraverser = mapper.apply((Iterable<T>) inbox);
        }

        if (emitFromTraverser(outputTraverser)) {
            inbox.clear();
            closeTraverser();
        }
    }

    @Override
    public void close() throws Exception {
        closeTraverser();
    }

    @Override
    public boolean closeIsCooperative() {
        return true;
    }

    private void closeTraverser() {
        if (outputTraverser != null) {
            try {
                outputTraverser.close();
            } catch (Exception ignored) {
            }
            outputTraverser = null;
        }
    }

}
